package ai;

import javax.swing.JOptionPane;

import board.Board;

public class AILogic{

	/*
	 * player = 1;
	 * int computer = 0;
	 * empty = -1;
	 * 
	 */

	int[][] currentBoard_refactored;
	int additionalX;
	int additionalY;
	int[][] currentBoard;
	
	Move moveLogic;
	boolean playerStarts;


	//constructor
	public AILogic(int[][] board,boolean playerStarts){

		this.playerStarts = playerStarts;
		moveLogic = new Move(playerStarts);
	}

	
	//------------------------------------------------|
	//		   	Corrent check return			   	  |
	//------------------------------------------------|

	/* normalizeReturnFromChecks()
	 *The following method will convert the "currentBoard_refactored" coordinater
	 *back to its representative coordinates in the currentBoard. This is a
	 *important method, which is implementet in the following methods:
	 *fourRowPc() fourRowPlayer() threeRowPc() threeRowPlayer()  
	 */
	private int[] normalizeReturnFromChecks(int[] returnedValue){
		int[] returnThis = new int[2];
		returnThis[0] = returnedValue[0]+additionalX;
		returnThis[1] = returnedValue[1]+additionalY;

		return returnThis;
	}

	public int[] figureOutWhatToDo(int[][] board){
		getBoard(board);
		return this.normalizeReturnFromChecks(moveLogic.checkWhatToDo(currentBoard_refactored, currentBoard));
	}
	
	//--------------------------------------------------------|
	//		   	Refactor the board to a smaller size	   	  |
	//--------------------------------------------------------|
	
	
	// returns the current playing board
	private void getBoard(int[][] board){
		currentBoard = board;
		this.refactorBoard();
	}	

	//Crops the array down to only the part we're gonna need :)

	private int[][] refactorBoard(){

		currentBoard_refactored = currentBoard;

		//the following code will allow us to minimize the array we have to work with:

		//this part decides how much of the original array/table that is in use
		int neededX_right = -1;
		int neededX_left = Integer.MAX_VALUE; 
		int neededY_bottom = -1;
		int neededY_top = Integer.MAX_VALUE;



		for(int i=0;i<currentBoard_refactored.length;i++){
			for(int y=0;y<currentBoard_refactored[i].length;y++){
				if (currentBoard_refactored[i][y] != -1) {
					if (y > neededX_right) neededX_right = y;
					else if (y < neededX_left) neededX_left = y;
					else if (i > neededY_bottom) neededY_bottom = i;
					else if (i < neededY_top) neededY_top = i;
				}
			}	
		}

		//this part sets the currentBoard to only the neccesary part of the array
		int[][] currentBoard_temp = new int[neededY_bottom-neededY_top][neededX_right-neededX_left];
		int counterX = -1;
		int counterY = -1;
		for(int i=neededY_top;i<=neededY_bottom;i++){
			counterY++;
			for(int y=neededX_left;y<=neededX_right;y++){
				counterX++;
				currentBoard_temp[counterY][counterX] = currentBoard_refactored[i][y]; 
			}
			counterX=-1;
		}
		//end

		additionalY = neededY_top;
		additionalX = neededX_left;
		currentBoard_refactored = currentBoard_temp;

		return currentBoard_refactored;
	}
}